package com.clippick.api;

import java.net.URI;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.clippick.common.GlobalValue;
import com.clippick.network.SocketIOClient;

public class ClipPickListener {

	public static interface ClipPickOnMessage {
		public void onMessage(String event, JSONArray arguments);

		public void onConnect();

		public void onError(Exception error);

		public void onDisConnect(int code, String reason);
	}

	public static ClipPickOnMessage socketMessage;
	public static boolean IS_CONNECT = false;

	// common
	private Context mContext;
	private static SharedPreferences pref;
	private static Editor editor;
	public ClipPickOnMessage callback;

	// connectivity count
	public static int SOCKET_COUNT = 0;

	// IS_CONNECTING
	public static boolean IS_CONNECTING = true;

	// callback method
	public static SocketIOClient.Handler listener = new SocketIOClient.Handler() {
		@Override
		public void onError(Exception error) {
			socketMessage.onError(error);
			IS_CONNECT = false;
			IS_CONNECTING = false;
		}

		@Override
		public void onDisconnect(int code, String reason) {
			socketMessage.onDisConnect(code, reason);
			IS_CONNECT = false;
			IS_CONNECTING = false;
		}

		@Override
		public void onConnect() {
			socketMessage.onConnect();
			IS_CONNECT = true;
			IS_CONNECTING = false;
		}

		@Override
		public void on(String event, JSONArray arguments) {
			socketMessage.onMessage(event, arguments);
			IS_CONNECTING = false;
		}
	};

	public static SocketIOClient client = new SocketIOClient(
			URI.create("http://clippick.com:8124"), listener);

	public ClipPickListener(Context context) {
		mContext = context;
		pref = mContext.getSharedPreferences(GlobalValue.KEY,
				Context.MODE_PRIVATE);
		editor = mContext.getSharedPreferences(GlobalValue.KEY,
				Context.MODE_PRIVATE).edit();
	}

	public void instance() {
		client = new SocketIOClient(URI.create("http://clippick.com:8124"),
				listener);
	}

	public void connect() {
		client.connect();
	}
	
	public static void sendSocketMessage(String email) {
		String time = GlobalValue.UPDATE_TIME;
		try {
			JSONArray arguments = new JSONArray();
			JSONObject second = new JSONObject();
			second.put("email", email);
			second.put("up_time", time);
			arguments.put(second);

			client.emit("clippick-params", arguments);
		} catch (Exception e) {
			e.printStackTrace();
		}
	
	}

	public static void sendSocketMessage() {
		String email = pref.getString("UserID", "");
		String time = GlobalValue.UPDATE_TIME;
		try {
			JSONArray arguments = new JSONArray();
			JSONObject second = new JSONObject();
			second.put("email", email);
			second.put("up_time", time);
			arguments.put(second);

			client.emit("clippick-params", arguments);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
